package com.cloudspace.jindun.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudspace.jindun.R;
import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.activity.dianhua.CallInOrOut;
import com.cloudspace.jindun.config.Configs;
import com.cloudspace.jindun.dao.PhoneRecord;
import com.cloudspace.jindun.net.manager.ImageManager;
import com.cloudspace.jindun.utils.ObjUtil;
import com.cloudspace.jindun.utils.TimeUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;


/**
 * Created by zengxianhua on 16/12/31.
 */

public class HistoryContactAdapter extends BaseAdapter {
    private List<PhoneRecord> datas;


    @Override
    public int getCount() {
        if (ObjUtil.isEmpty(datas)){
            return 0;
        }

        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        if (getCount() == 0){
            return null;
        }

        return datas.get(position);
    }

    public void setDatas(List<PhoneRecord> data){
        sortData(data);


        datas = data;
        notifyDataSetChanged();
    }

    private void sortData(List<PhoneRecord> data) {
        if (ObjUtil.isEmpty(data)){
            return;
        }

        Collections.sort(data, new Comparator<PhoneRecord>() {
            @Override
            public int compare(PhoneRecord lhs, PhoneRecord rhs) {
                if (lhs == null || rhs == null ||
                        TextUtils.isEmpty(lhs.getTime()) || TextUtils.isEmpty(rhs.getTime())){
                    return -1;
                }

                long ltime = lhs.getTimestamp();
                long rtime = rhs.getTimestamp();

                return (int) (rtime-ltime);
            }
        });
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(JindunApp.getInstance()).inflate(R.layout.item_call_history, null);
            holder.callDate = (TextView) convertView.findViewById(R.id.call_date);
            holder.callTime= (TextView) convertView.findViewById(R.id.call_time);
            holder.callState = (ImageView) convertView.findViewById(R.id.call_state);
            holder.userIcon= (ImageView) convertView.findViewById(R.id.user_icon);
            holder.userName = (TextView) convertView.findViewById(R.id.user_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        PhoneRecord record = datas.get(position);

        if (CallInOrOut.INCALL == record.getCallfrom()){
            holder.callState.setImageResource(R.drawable.huru);
        }else if (CallInOrOut.OUTCALL == record.getCallfrom()){
            holder.callState.setImageResource(R.drawable.huchu);
        }else if (CallInOrOut.CALLFAIL == record.getCallfrom()){
            holder.callState.setImageResource(R.drawable.weijie);
        }


        String dayDiff = TimeUtils.getFitTimeSpanByNow(record.getTime(), 1);
        if (TextUtils.isEmpty(dayDiff)){
            record.setDaystr(JindunApp.getInstance().getString(R.string.today));
        }else if ("1".equals(dayDiff)){
            record.setDaystr(JindunApp.getInstance().getString(R.string.yesterday));
        }

        holder.userName.setText(record.getName());
        holder.callDate.setText(record.getDaystr());
        holder.callTime.setText(record.getHm());

        if (position == 0){
            holder.callDate.setVisibility(View.VISIBLE);
        }else{
            PhoneRecord preRecord = datas.get(position-1);
            if (TextUtils.equals(record.getDaystr(), preRecord.getDaystr())){
                holder.callDate.setVisibility(View.GONE);
            }
        }

        ImageManager.loadCircleImage(JindunApp.getInstance(), holder.userIcon, record.getImage(), Configs.IMAGE_CORNER_RADIUS);

        return convertView;
    }



    static class ViewHolder {
        private TextView callDate;
        private TextView userName;
        private TextView callTime;
        private ImageView callState;
        private ImageView userIcon;
    }
}